<?php
/**
 * Created by Veyron
 * User: Vy
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/1/14
 * Time: 15:44
 */
namespace app\admin\controller;
use app\common\model\mysql\AdminUser;

class Login extends AdminBase {
    /**
     * 登录页面
     */
    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect(url('index/index'));
        }
    }

    public function index(){
        return view();
    }

    /**
     * AJAX 登录验证
     */
    public function check(){
        if(!$this->app->request->isPost()){
            return show(config('status.error'),'请求方式错误');
        }
        //参数效验 1.原生方式 2.TP6 验证机制
        $username = $this->app->request->param('username','','trim');
        $password = $this->app->request->param('password','','trim');
        $captcha  = $this->app->request->param('captcha','','trim');

        $data = [
            'username' => $username,
            'password' => $password,
            'captcha'  => $captcha,
        ];
        $validate = new \app\admin\validate\AdminUser();
        if(!$validate->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        try {
            $adminUserBusinessObj = new \app\admin\business\AdminUser();
            $result = $adminUserBusinessObj->login($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }

        if($result){
            return show(config('status.success'),'登录成功');
        }else{
            return show(config('status.error'),'登录失败');
        }

    }
}
